<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\logic\CategoryLogic;
use app\admin\model\Category;
use app\admin\validate\CategoryValidate;
use plugin\saiadmin\basic\BaseController;
use support\Redis;
use support\Request;
use support\Response;

/**
 * 商品分类表控制器
 */
class CategoryController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic    = new CategoryLogic();
        $this->validate = new CategoryValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['id', ''],
            ['category_name', ''],
        ]);
        $query = $this->logic->search($where);
        $data  = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 数据改变后执行
     * @param string $type 类型
     * @param $args
     */
    protected function afterChange(string $type, $args): void
    {
        // 重新获取所有分类，缓存到redis中
        $categoryList = Category::select()->toArray();
        // 缓存到redis中
        Redis::set('category_list', json_encode($categoryList));
    }

}
